home *** CD-ROM | disk | FTP | other *** search
- Path: maestro.inav.net!dkelly
- From: dkelly@blue.weeg.uiowa.edu (Dave Kelly)
- Newsgroups: comp.lang.c++
- Subject: Typecasting a class? Help!
- Date: 28 Mar 96 21:34:58 GMT
- Organization: Internet Navigator, Inc.
- Message-ID: <dkelly.828048898@maestro.inav.net>
- NNTP-Posting-Host: maestro.inav.net
-
- OK, we all know we can do this:
-
- x = 6;
- printf("x is equal to %d\n",x);
-
- and it works fine. But what I want to do is this:
-
- class MyInt
- {
- public:
- int iTheValue;
- operator int() { return iTheValue; };
- void SetValue(int iNewValue) { iTheValue = iNewValue; };
- };
-
- MyInt clInteger;
- clInteger.SetValue(6);
- printf("clInteger is equal to %d\n", clInteger);
-
-
- But it doesn't work unless I do this:
-
- printf("clInteger is equal to %d\n", (int)clInteger);
-
- Is there any way I can get an instance of MyInt to be seen as
- and integer to printf without having to explicitly cast it as above?
-
- I'm not sure that this can even be done, but any information will be
- appreciated!
-
- Thanks!
- -Dave
- --
- ###### ####### ##### ####### ---------------------------------------
- # # # # # # # . . # | Another tasty e-mail message from |
- #### # # # ###### # ### | Dave Kelly |
- # # # # # # # `-' # #/ | dkelly@inav.net |
- ###### # # ###### ####### ; | URL=http://www.inav.net/~dkelly/ |
- _/ ---------------------------------------
- Electronic Mail /_/
-